package com.game.thrones.controller.webpage;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.basic.BasicOptionPaneUI;
import javax.validation.Valid;

import com.game.thrones.service.web.WebEndemicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.game.thrones.constant.Errors;
import com.game.thrones.controller.BaseController;
import com.game.thrones.controller.input.DataInput;
import com.game.thrones.controller.input.StringInput;
import com.game.thrones.controller.output.BaseOutput;
import com.game.thrones.controller.output.PageOutput;
import com.game.thrones.controller.output.ResultResponseBeanUtil;
import com.game.thrones.domain.KbmContent;
import com.game.thrones.domain.webpage.WebNewsDO;
import com.game.thrones.exception.GameGlobalException;
import com.game.thrones.service.web.WebNewsService;
import com.game.thrones.util.OSSUploadUtil;
import com.game.thrones.util.StringUtils;
//
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;

//@Api(value = "网站-新闻", description = "网站-新闻-田春阳", produces = "application/json")
@Controller
public class WebNewsController extends BaseController {
	private Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	WebNewsService webNewsService;
	@Autowired
	WebEndemicService webEndemicService;
//	@ApiOperation(value = "根据类型获取列表", notes = "根据类型获取列表")
	@ResponseBody
	@RequestMapping(value = "/web/news/findListByType", method = RequestMethod.POST)
	public BaseOutput<List<WebNewsDO>> findListByType(HttpServletRequest request,
			@Valid @RequestBody StringInput input) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		String type = input.getStr();
		List<WebNewsDO> entityList = webNewsService.findListByType(kbmContent, type);
		List<WebNewsDO> outList = new ArrayList<WebNewsDO>();
		for (WebNewsDO entity : entityList) {
			if("2-13-1".equals(type)||"2-14-1".equals(type)
					||"6-1-1".equals(type)
					||"2-1".equals(type)
					||"2-2".equals(type)
					||"2-3".equals(type)
					||"2-4".equals(type)){

			}else{
				entity.setContent("");
			}
			setWebNewsImgOutput(entity);
			outList.add(entity);
		}
		return ResultResponseBeanUtil.success(outList);
	}

//	@ApiOperation(value = "根据类型获取分页列表", notes = "根据类型获取分页列表")
	@ResponseBody
	@RequestMapping(value = "/web/news/findPageListByType", method = RequestMethod.POST)
	public PageOutput<WebNewsDO> findPageListByType(HttpServletRequest request,
			@Valid @RequestBody WebNewsPageInput input) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		input.setKbmContent(kbmContent);
		List<WebNewsDO> entityList = webNewsService.findByPage(input);
		Integer total = webNewsService.countByPage(input);
        List<WebNewsDO> outputList=new ArrayList<WebNewsDO>();
        for (WebNewsDO entity : entityList) {
			if("2-13-1".equals(input.getType())||"2-14-1".equals(input.getType())
					||"6-1-1".equals(input.getType())
					||"2-1".equals(input.getType())
					||"2-2".equals(input.getType())
					||"2-3".equals(input.getType())
					||"2-4".equals(input.getType())){

			}else{
				entity.setContent("");
			}
            setWebNewsImgOutput(entity);
            outputList.add(entity);
		}
		return ResultResponseBeanUtil.pageSuccess(outputList, input.getSize(), input.getPage(), total);
	}

//	@ApiOperation(value = "根据类型获取", notes = "根据类型获取")
	@ResponseBody
	@RequestMapping(value = "/web/news/findOneByType", method = RequestMethod.POST)
	public BaseOutput<WebNewsDO> findOneByType(HttpServletRequest request, @Valid @RequestBody StringInput input) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		WebNewsDO entity = webNewsService.findOneByType(kbmContent, input.getStr());
		if(entity==null){
			throw new GameGlobalException(Errors.DATA_NOTEXIST);
		}
		setWebNewsImgOutput(entity);
		return ResultResponseBeanUtil.success(entity);
	}

//	@ApiOperation(value = "根据ID获取", notes = "根据ID获取")
	@ResponseBody
	@RequestMapping(value = "/web/news/findOneById", method = RequestMethod.POST)
	public BaseOutput<WebNewsOutput> findOneById(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		WebNewsDO entity = webNewsService.findOneById(kbmContent, input.getDataId());
        if(entity==null){
            throw new GameGlobalException(Errors.DATA_NOTEXIST);
        }
        setWebNewsImgOutput(entity);

		WebNewsOutput output = new WebNewsOutput();
		BeanUtils.copyProperties(entity,output);
		WebNewsDO prev = webNewsService.findOneBySql(kbmContent,"select * from web_news where type='"+entity.getType()+"' and id<"+entity.getId());
		if(prev!=null) {
			output.setPrevId(prev.getId());
			output.setPrevTitle(prev.getTitle());
		}

		WebNewsDO next = webNewsService.findOneBySql(kbmContent,"select * from web_news where type='"+entity.getType()+"' and id>"+entity.getId());
		if(next!=null) {
            output.setNextId(next.getId());
            output.setNextTitle(next.getTitle());
        }
		return ResultResponseBeanUtil.success(output);
	}

//	@ApiOperation(value = "新增", notes = "新增")
	@ResponseBody
	@RequestMapping(value = "/web/news/add", method = RequestMethod.POST)
	public BaseOutput<Void> add(HttpServletRequest request, @Valid @RequestBody WebNewsDO input) {
		KbmContent kbmContent = getCurrentInfo(request);
		if (kbmContent == null) {
			throw new GameGlobalException(Errors.MODIFY_ERROR);
		}
		input.setKbmContent(kbmContent);
		webNewsService.add(input);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "修改", notes = "修改")
	@ResponseBody
	@RequestMapping(value = "/web/news/update", method = RequestMethod.POST)
	public BaseOutput<Void> update(HttpServletRequest request, @Valid @RequestBody WebNewsDO input) {
		KbmContent kbmContent = getCurrentInfo(request);
		if (kbmContent == null) {
			throw new GameGlobalException(Errors.MODIFY_ERROR);
		}
		input.setKbmContent(kbmContent);
		webNewsService.update(input);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "删除", notes = "删除")
	@ResponseBody
	@RequestMapping(value = "/web/news/delete", method = RequestMethod.POST)
	public BaseOutput<Void> delete(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		WebNewsDO entity = webNewsService.findOneById(kbmContent, input.getDataId());
		if (entity == null) {
			throw new GameGlobalException(Errors.DATA_NOTEXIST);
		}
		entity.setKbmContent(kbmContent);
		webNewsService.del(entity);
		return ResultResponseBeanUtil.success();
	}
//    @ApiOperation(value = "专家团队列表", notes = "专家团队列表")
    @ResponseBody
    @RequestMapping(value = "/web/news/expertTeamList", method = RequestMethod.POST)
    public BaseOutput<List> expertTeamList(HttpServletRequest request ) {
        KbmContent kbmContent = webEndemicService.findKbmContent(request);
        List<WebNewsDO> outpuList=webNewsService.findBySql(kbmContent,"select * from web_news where type in('2-12','2-13','2-14','2-15','2-20','2-21','2-22','2-23')");
        for(WebNewsDO entity:outpuList) {
            //WebNewsDO entity = webNewsService.findOneByType(kbmContent, input.getStr());
            if (entity == null) {
                throw new GameGlobalException(Errors.DATA_NOTEXIST);
            }
            setWebNewsImgOutput(entity);
        }
        return ResultResponseBeanUtil.success(outpuList);
    }

    /**
     * 设置实体图片信息
     * @param entity
     */
    private void setWebNewsImgOutput(WebNewsDO entity){
        String img1 = entity.getImg1();
        String img2 = entity.getImg2();
        String img3 = entity.getImg3();

        if (StringUtils.isNotEmpty(img1)) {
            entity.setImg1Url(OSSUploadUtil.getUrl(img1));
        }
        if (StringUtils.isNotEmpty(img2)) {
            entity.setImg2Url(OSSUploadUtil.getUrl(img2));
        }
        if (StringUtils.isNotEmpty(img3)) {
            entity.setImg3Url(OSSUploadUtil.getUrl(img3));
        }
    }
}
